<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back"
                  @click="$router.back()" />
        <span class="iconfont iconnew new"
              @click="$router.push('/index')"></span>
      </div>
      <span :class="{active:post.has_follow}"
            @click='followUser'>{{post.has_follow?'已关注':'关注'}}</span>
    </div>
    <div class="detail">
      <!-- 访问对象的不存在的属性>>undefined -->
      <div class="title">{{post.title}}</div>
      <div class="desc">
        <span>{{post.user && post.user.nickname}}</span> &nbsp;&nbsp;
        <span>{{post.create_date | singledateFormat}}</span>
      </div>
      <div class="content"
           v-html='post.content'
           v-if='post.type == 1'>
      </div>
      <!-- 播放器的基本属性配置 
      controls:是否显示播放器控制面板，如果没有控制面板，则播放器不可见
      src:视频文件的路径
      poster:设置第一帧的画面--封面
      autoplay:自动播放，谷歌不支持
      loop:循环播放-->
      <video :src="axios.defaults.baseURL + post.content"
             controls
             v-if='post.type==2'></video>
      <div class="opt">
        <span class="like"
              :class="{active:post.has_like}"
              @click="likeArticle">
          <van-icon name="good-job-o" />{{post.like_length || 0}}
        </span>
        <span class="chat">
          <van-icon name="chat"
                    class="w" />微信
        </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="../assets/logo.png"
               alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <!-- 底部评论块 -->
    <commentFooter :article='post'></commentFooter>
  </div>
</template>

<script>
import { getPostById, likeThisArticle } from '@/apis/post.js'
import { followUser, unfollowUser } from '@/apis/user.js'
import { singledateFormat } from '@/utils/myfilters.js'
import axios from '@/utils/myaxios.js'
import commentFooter from '@/components/commentFooter.vue'
export default {
  data () {
    return {
      axios,
      post: {
      }
    }
  },
  components: {
    commentFooter
  },
  // 在vue实例创建好，模板编译前
  async activated () {
    setTimeout(() => {
      window.scrollTo(0, 0)
    }, 0);
    let id = this.$route.params.id
    // 根据id获取文章的详情，实现文章详情的动态渲染
    let res = await getPostById(id)
    console.log(res);
    this.post = res.data.data
  },
  filters: {
    singledateFormat
  },
  methods: {
    // 文章点赞
    async likeArticle () {
      let res = await likeThisArticle(this.post.id)
      console.log(res);
      // 给出提示
      this.$toast.success(res.data.message)
      // 变化数量
      if (this.post.has_like) { // true:代表本次应该是取消
        this.post.like_length--
      } else {
        this.post.like_length++
      }
      // 切换页面样式
      this.post.has_like = !this.post.has_like
    },
    // 关注用户
    async followUser () {
      try {
        let id = this.post.user.id
        // 判断当前的关注状态，如果是未关注，则本次单击进行用户的关注，如果是已关注状态，那么本次单击是取消用户关注
        let res
        if (this.post.has_follow) { // true:说明已关注
          // 取消关注用户
          res = await unfollowUser(id)
        } else { // false:说明未关注
          // 关注用户
          res = await followUser(id)
        }
        // 实现页面刷新
        this.post.has_follow = !this.post.has_follow
        // 给出用户提示
        this.$toast.success(res.data.message)
      }
      catch (err) { }
    }
  }
}
</script>

<style lang='less' scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    border: 1px solid #f00;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
  .active {
    border: 1px solid #ccc;
    background-color: transparent;
    color: #333;
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
      display: block;
    }
  }
  video {
    width: 100%;
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .active {
    color: red;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>
